home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ETO Development Tools 4
/
ETO Development Tools 4.iso
/
Tools - Objects
/
MacApp
/
MacApp 3.0a2
/
CPlusIncludes
/
PascalString.h
< prev
next >
Wrap
Text File
|
1991-05-01
|
14KB
|
666 lines
#ifndef __PASCALSTRING__
#define __PASCALSTRING__
#ifndef __STRING__
#include "String.h"
#endif
#ifndef __TYPES__
#include "Types.h"
#endif
// Forward declaration for all the string classes.
struct String;
struct Str255;
struct Str63;
struct Str32;
struct Str31;
typedef const Str255& ConstStr255Param;
typedef const Str63& ConstStr63Param;
typedef const Str32& ConstStr32Param;
typedef const Str31& ConstStr31Param;
#ifndef __OSUTILS__
#include "OSUtils.h"
#endif
typedef unsigned char SimpleStr255[256], SimpleStr63[64], SimpleStr32[33],
SimpleStr31[32], SimpleStr27[28], SimpleStr15[16], * SimpleStringPtr, ** SimpleStringHandle;
// Some constants defining the length of each of the string types.
const short kLengthByte = 1;
const short kBaseLen = 2;
const short kStr255Len = 255;
const short kStr63Len = 63;
const short kStr32Len = 32;
const short kStr31Len = 31;
// Some external function declarations so that we don't have to include more than
// the minimal set of header files.
pascal char* PLSTRSTR(const String& str1,
const String& str2); // From PaslibIntf.p
typedef struct String *StringPtr, ** StringHandle;
struct String
{
public:
unsigned char fStr[kBaseLen];
protected:
void InsertHelper(const String& insStr,
short pos,
short maxLength);
void InsertHelper(const char* insStr,
short pos,
short maxLength);
public:
// Basic length method, inherited by all derived classes. Define one that returns a
// reference. Can be used as an lvalue and only can be applied to non-const Strings.
inline unsigned char& Length()
{
return fStr[0];
} // for non-const String
inline unsigned char Length() const
{
return fStr[0];
} // for const String
inline Boolean IsEmpty()
{
return fStr[0] <= 0;
}
inline Boolean IsEmpty() const
{
return fStr[0] <= 0;
}
// Character selector operator.
unsigned char& operator[](short pos); // for non-const String
// !!! try to inline later
inline unsigned char operator[](short pos) const
{
return fStr[pos];
} // for const String
// Return the String as a CString. Used in debugging to fprintf a String.
operator char*() const;
// Relational operators that are inherited by all the derived string types. Three of
// each so that literal C Strings can be conveniently used for one of the operators as
// well as two of the derive classes as operators. These are declared here but defined
// below all the string classes because they use constructors for Str255 and its class
// definition has not been encountered yet.
inline friend Boolean operator==(const String& s1,
const char* s2);
inline friend Boolean operator==(const char* s1,
const String& s2);
inline friend Boolean operator==(const String& s1,
const String& s2);
inline friend Boolean operator!=(const String& s1,
const char* s2);
inline friend Boolean operator!=(const char* s1,
const String& s2);
inline friend Boolean operator!=(const String& s1,
const String& s2);
inline friend Boolean operator>(const String& s1,
const char* s2);
inline friend Boolean operator>(const char* s1,
const String& s2);
inline friend Boolean operator>(const String& s1,
const String& s2);
inline friend Boolean operator<(const String& s1,
const char* s2);
inline friend Boolean operator<(const char* s1,
const String& s2);
inline friend Boolean operator<(const String& s1,
const String& s2);
inline friend Boolean operator>=(const String& s1,
const char* s2);
inline friend Boolean operator>=(const char* s1,
const String& s2);
inline friend Boolean operator>=(const String& s1,
const String& s2);
inline friend Boolean operator<=(const String& s1,
const char* s2);
inline friend Boolean operator<=(const char* s1,
const String& s2);
inline friend Boolean operator<=(const String& s1,
const String& s2);
// Concatenation operator that are inherited by all the derived string types. Three
// of each so that literal C Strings can be conveniently used for one of the operators
// as well as using any two classes derived from String.
friend Str255 operator+(const String& s1,
const char* s2);
friend Str255 operator+(const char* s1,
const String& s2);
friend Str255 operator+(const String& s1,
const String& s2);
// Methods that mimic the Pascal builtin string functions for Pos, Insert and Delete.
// Note that insert and copy is implemented in the derived classes.
inline unsigned char Pos(const char* subStr);
inline unsigned char Pos(const String& subStr);
inline void Delete(short pos,
short length);
};
struct Str255 : String
{
friend struct Str63;
friend struct Str31;
private:
unsigned char fData[kStr255Len - 1];
public:
inline Str255();
inline Str255(const Str255& str);
inline Str255(const Str63& str);
inline Str255(const Str31& str);
Str255(const char* str);
inline Str255(const SimpleStr255& str);
// Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
inline void Insert(const String& str,
short pos);
inline void Insert(const char* str,
short pos);
Str255 Copy(short pos, short length);
// Concatenation operator
Str255& operator +=(const String& str);
Str255& operator +=(const char* str);
Str255& operator +=(const char ch);
};
struct Str63 : String
{
friend struct Str255;
friend struct Str31;
private:
unsigned char fData[kStr63Len - 1];
public:
inline Str63();
inline Str63(const Str255& str);
inline Str63(const Str63& str);
inline Str63(const Str31& str);
inline Str63(const SimpleStr63& str);
Str63(const char* str);
// Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
inline void Insert(const String& str,
short pos);
inline void Insert(const char* str,
short pos);
Str63 Copy(short pos, short length);
// Concatenation operator
Str63& operator +=(const String& str);
Str63& operator +=(const char* str);
Str63& operator +=(const char ch);
};
struct Str32 : String
{
friend struct Str255;
friend struct Str63;
private:
unsigned char fData[kStr32Len - 1];
public:
inline Str32();
inline Str32(unsigned char length)
{
fStr[0] = length;
}
inline Str32(const Str255& str);
inline Str32(const Str63& str);
inline Str32(const Str32& str);
inline Str32(const SimpleStr32& str);
Str32(const char* str);
// Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
inline void Insert(const String& str,
short pos);
inline void Insert(const char* str,
short pos);
Str32 Copy(short pos, short length);
// Concatenation operator
Str32& operator +=(const String& str);
Str32& operator +=(const char* str);
Str32& operator +=(const char ch);
};
struct Str31 : String
{
friend struct Str255;
friend struct Str63;
friend struct Str32;
private:
unsigned char fData[kStr31Len - 1];
public:
inline Str31();
inline Str31(unsigned char length)
{
fStr[0] = length;
}
inline Str31(const Str255& str);
inline Str31(const Str63& str);
inline Str31(const Str32& str);
inline Str31(const Str31& str);
inline Str31(const SimpleStr31& str);
Str31(const char* str);
// Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
inline void Insert(const String& str,
short pos);
inline void Insert(const char* str,
short pos);
Str31 Copy(short pos, short length);
// Concatenation operator
Str31& operator +=(const String& str);
Str31& operator +=(const char* str);
Str31& operator +=(const char ch);
};
// Function definitions for relational operators.
inline Boolean operator==(const String& s1,
const char* s2)
{
return RelString(s1, Str255(s2), false, true) == 0;
}
inline Boolean operator==(const char* s1,
const String& s2)
{
return RelString(Str255(s1), s2, false, true) == 0;
}
inline Boolean operator==(const String& s1,
const String& s2)
{
return RelString(s1, s2, false, true) == 0;
}
inline Boolean operator!=(const String& s1,
const char* s2)
{
return RelString(s1, Str255(s2), false, true) != 0;
}
inline Boolean operator!=(const char* s1,
const String& s2)
{
return RelString(Str255(s1), s2, false, true) != 0;
}
inline Boolean operator!=(const String& s1,
const String& s2)
{
return RelString(s1, s2, false, true) != 0;
}
inline Boolean operator>(const String& s1,
const char* s2)
{
return RelString(s1, Str255(s2), false, true) > 0;
}
inline Boolean operator>(const char* s1,
const String& s2)
{
return RelString(Str255(s1), s2, false, true) > 0;
}
inline Boolean operator>(const String& s1,
const String& s2)
{
return RelString(s1, s2, false, true) > 0;
}
inline Boolean operator<(const String& s1,
const char* s2)
{
return RelString(s1, Str255(s2), false, true) < 0;
}
inline Boolean operator<(const char* s1,
const String& s2)
{
return RelString(Str255(s1), s2, false, true) < 0;
}
inline Boolean operator<(const String& s1,
const String& s2)
{
return RelString(s1, s2, false, true) < 0;
}
inline Boolean operator>=(const String& s1,
const char* s2)
{
return RelString(s1, Str255(s2), false, true) >= 0;
}
inline Boolean operator>=(const char* s1,
const String& s2)
{
return RelString(Str255(s1), s2, false, true) >= 0;
}
inline Boolean operator>=(const String& s1,
const String& s2)
{
return RelString(s1, s2, false, true) >= 0;
}
inline Boolean operator<=(const String& s1,
const char* s2)
{
return RelString(s1, Str255(s2), false, true) <= 0;
}
inline Boolean operator<=(const char* s1,
const String& s2)
{
return RelString(Str255(s1), s2, false, true) <= 0;
}
inline Boolean operator<=(const String& s1,
const String& s2)
{
return RelString(s1, s2, false, true) <= 0;
}
// Function definitions for Str255 constructors.
inline Str255::Str255()
{
}
inline Str255::Str255(const Str255& str)
{
memcpy(fStr, str.fStr, str.Length() + kLengthByte);
}
inline Str255::Str255(const Str63& str)
{
memcpy(fStr, str.fStr, str.Length() + kLengthByte);
}
inline Str255::Str255(const Str31& str)
{
memcpy(fStr, str.fStr, str.Length() + kLengthByte);
}
inline Str255::Str255(const SimpleStr255& str)
{
memcpy(fStr, str, kStr255Len + kLengthByte);
}
// Fuction definitions for Str63 constructors.
inline Str63::Str63()
{
}
inline Str63::Str63(const Str255& str)
{
// Truncate the Str255 to 63 bytes if necessary.
Length() = str.Length() > kStr63Len ? kStr63Len : str.Length();
memcpy(fStr, str.fStr, Length() + kLengthByte);
}
inline Str63::Str63(const Str63& str)
{
memcpy(fStr, str.fStr, str.Length() + kLengthByte);
}
inline Str63::Str63(const Str31& str)
{
memcpy(fStr, str.fStr, str.Length() + kLengthByte);
}
inline Str63::Str63(const SimpleStr63& str)
{
memcpy(fStr, str, kStr63Len + kLengthByte);
}
// Function definitions for Str32 constructors.
inline Str32::Str32()
{
}
inline Str32::Str32(const Str255& str)
{
// Truncate the Str255 to 32 bytes if necessary.
Length() = str.Length() > kStr32Len ? kStr32Len : str.Length();
memcpy(fStr, str.fStr, Length() + kLengthByte);
}
inline Str32::Str32(const Str63& str)
{
// Truncate the Str63 to 32 bytes if necessary.
Length() = str.Length() > kStr32Len ? kStr32Len : str.Length();
memcpy(fStr, str.fStr, Length() + kLengthByte);
}
inline Str32::Str32(const Str32& str)
{
memcpy(fStr, str.fStr, str.Length() + kLengthByte);
}
inline Str32::Str32(const SimpleStr32& str)
{
memcpy(fStr, str, kStr31Len + kLengthByte);
}
// Function definitions for Str31 constructors.
inline Str31::Str31()
{
}
inline Str31::Str31(const Str255& str)
{
// Truncate the Str255 to 31 bytes if necessary.
Length() = str.Length() > kStr31Len ? kStr31Len : str.Length();
memcpy(fStr, str.fStr, Length() + kLengthByte);
}
inline Str31::Str31(const Str63& str)
{
// Truncate the Str63 to 31 bytes if necessary.
Length() = str.Length() > kStr31Len ? kStr31Len : str.Length();
memcpy(fStr, str.fStr, Length() + kLengthByte);
}
inline Str31::Str31(const Str31& str)
{
memcpy(fStr, str.fStr, str.Length() + kLengthByte);
}
inline Str31::Str31(const SimpleStr31& str)
{
memcpy(fStr, str, kStr31Len + kLengthByte);
}
// Function definitions for methods to mimic the Pascal builtin string operators.
inline unsigned char String::Pos(const char* subStr)
{
char* ptr;
ptr = strstr((const char*) & fStr[1], subStr);
return ptr != NULL ? ptr - (char*)fStr : 0;
}
inline unsigned char String::Pos(const String& subStr)
{
char* ptr;
ptr = PLSTRSTR(*this, subStr);
return ptr != NULL ? ptr - (char*)fStr : 0;
}
inline void String::Delete(short pos,
short length)
{
memcpy(&fStr[pos], &fStr[pos + length], Length() - (pos + length) + kLengthByte);
fStr[0] -= length;
}
inline void Str255::Insert(const String& str,
short pos)
{
InsertHelper(str, pos, kStr255Len);
}
inline void Str255::Insert(const char* str,
short pos)
{
InsertHelper(str, pos, kStr255Len);
}
inline void Str63::Insert(const String& str,
short pos)
{
InsertHelper(str, pos, kStr63Len);
}
inline void Str63::Insert(const char* str,
short pos)
{
InsertHelper(str, pos, kStr63Len);
}
inline void Str32::Insert(const String& str,
short pos)
{
InsertHelper(str, pos, kStr32Len);
}
inline void Str32::Insert(const char* str,
short pos)
{
InsertHelper(str, pos, kStr32Len);
}
inline void Str31::Insert(const String& str,
short pos)
{
InsertHelper(str, pos, kStr31Len);
}
inline void Str31::Insert(const char* str,
short pos)
{
InsertHelper(str, pos, kStr31Len);
}
#endif